package com.shardingsphereproxy.springbootshardingsphereproxy.service;

import com.alibaba.fastjson.JSONObject;
import com.shardingsphereproxy.springbootshardingsphereproxy.dao.DictMapper;
import com.shardingsphereproxy.springbootshardingsphereproxy.dao.OrderDetailMapper;
import com.shardingsphereproxy.springbootshardingsphereproxy.dao.OrderMapper;
import com.shardingsphereproxy.springbootshardingsphereproxy.dao.UserMapper;
import com.shardingsphereproxy.springbootshardingsphereproxy.entity.Dict;
import com.shardingsphereproxy.springbootshardingsphereproxy.entity.Order;
import com.shardingsphereproxy.springbootshardingsphereproxy.entity.OrderDetail;
import com.shardingsphereproxy.springbootshardingsphereproxy.entity.User;
import com.shardingsphereproxy.springbootshardingsphereproxy.utils.JedisUtil3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Random;
import java.util.UUID;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/10/23 23:39
 */
@Slf4j
@Service
public class TestService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private DictMapper dictMapper;


    //读写分离测试
    //@Transactional
    public User queryUser(Integer id) {
        User insert = new User();
        insert.setUname(UUID.randomUUID().toString().replace("-", ""));
        userMapper.insert(insert);

        User user = userMapper.selectById(id);
        log.info(JSONObject.toJSONString(user));
        //事务测试
        //int n = 10 / 0;
        return user;
    }


    public void createOrder2() {
        //用户
        User insert = new User();
        insert.setUname("Test" + new Random().nextInt(100));
        userMapper.insert(insert);
        //订单
        Order order = new Order();

        order.setOrderNo(JedisUtil3.getOrderId());
        order.setUserId(insert.getId());
        order.setAmount(new BigDecimal(500));
        orderMapper.insert(order);
        for (int i = 1; i <= 2; i++) {
            OrderDetail detail = new OrderDetail();

            detail.setUserId(insert.getId());
            detail.setOrderNo(order.getOrderNo());
            detail.setPrice(new BigDecimal(100 * new Random().nextInt(20)));
            detail.setCount(i);
            orderDetailMapper.insert(detail);
        }

        Dict dict = new Dict();

        dict.setDictType(order.getOrderNo());
        dictMapper.insert(dict);

        log.info("#################{}###################",order.getOrderNo());

    }


}
